<div class="content">
    <div class="right">
        <div id="sf_admin_container">
            <div id="search_box">
                <div class="sf_admin_filters">
                    <form action="/customer_care/download?page=1" method="POST">
                        <fieldset>
                            <h2>Tìm kiếm</h2>
                            <div class="form-row">
                                <label>Số điện thoại: </label>
                                <input type="text" name="phone_number" value ="<?php echo isset($phoneNumber) ? $phoneNumber : null ?> "/>
                            </div>
                            <div class="form-row">
                                <label>Nguồn: </label>
                                <select name="download_source">
                                    <option <?php if ($downloadSource == "All") {
    echo("selected");
} ?>>All</option>
                                    <option <?php if ($downloadSource == "WEB") {
    echo("selected");
} ?>>WEB</option>
                                    <option <?php if ($downloadSource == "WAP") {
    echo("selected");
} ?>>WAP</option>
                                    <option <?php if ($downloadSource == "SMS") {
    echo("selected");
} ?>>SMS</option>
                                </select>
                            </div>
                            <div class="form-row">
                                <label for="filters_created_datetime">Ngày tạo</label>
                                <div class="content">
                                    <?php
                                    $name = 'created_datetime';
                                    $value1 = isset($createdDatetimeFrom) ? $createdDatetimeFrom : null;
                                    $value2 = isset($createdDatetimeTo) ? $createdDatetimeTo : null;
                                    $options = array(
                                        'rich' => true,
                                        'withtime' => true,
                                        'calendar_button_img' => '/sf/sf_admin/images/date.png',
                                    );
                                    $options = _parse_attributes($options);
                                    $before = _get_option($options, 'before', '');
                                    $middle = _get_option($options, 'middle', '');
                                    $after = _get_option($options, 'after', '');
                                    ?>
                                    <?php
                                    echo $before .
                                    input_date_tag($name . '_from', $value1, $options) .
                                    $middle;
                                    ?>
<?php
echo input_date_tag($name . '_to', $value2, $options) .
 $after;
?>

                                </div>
                            </div>
                            <div style="clear: both; margin-left: 120px;" class="form-row">
                                <input type="submit" style="width: 80px;" value="Tìm kiếm" name="filter"/>
                            </div>
                        </fieldset>
                    </form>
                </div>
            </div>
            <div id="sf_admin_content">
<?php if ($download): ?>
                    <table class="sf_admin_list" cellspacing="0">
                        <thead>
                            <tr>
                                <th>RingId</th>
                                <th>Số người tặng</th>
                                <th>Số người nhận</th>
                                <th>Tên</th>
                                <th>Nguồn</th>
                                <th>Ngày tải</th>
                                <th>Tải/Tặng</th>
                                <th>Giá</th>
                                <th>Gửi Wap Push</th>
                                <th>Số lần gửi</th>
                            </tr>
                        </thead>
                        <tbody>
                                    <?php for ($i = 0; $i < count($download); $i++): ?>
                                <tr class="sf_admin_row_<?php echo $i % 2 ?>">
                                    <td>
                                        <?php echo $download[$i]['ringtone_id']; ?>
                                    </td>
                                    <td>
                                        <?php echo $download[$i]['from_phone']; ?>
                                    </td>
                                    <td>
                                        <?php echo $download[$i]['to_phone']; ?>
                                    </td>
                                    <td>
                                        <?php echo $download[$i]['name']; ?>
                                    </td>
                                    <td>
                                        <?php echo $download[$i]['source_type']; ?>
                                    </td>
                                    <td>
                                        <?php echo $download[$i]['download_datetime']; ?>
                                    </td>
                                    <td>
                                        <?php
                                        //echo $download[$i]['status'];
                                        if ($download[$i]['from_phone'] == $download[$i]['to_phone']) {
                                            echo "Tải";
                                        } else {
                                            echo "Tặng";
                                        }
                                        ?>
                                    </td>
                                    <td>
                                        <?php echo $download[$i]['price']; ?>
                                    </td>
                                    <td>
                                        <a href="javascript:void(0);" onclick="if (confirm('Bạn có chắc muốn gửi wap push đến <?php echo $download[$i]['to_phone']; ?>?') == false) return false; new Ajax.Updater('sms_count_<?php echo $download[$i]['id']; ?>', '/customer_care/resendSms/id/<?php echo $download[$i]['id']; ?>', {asynchronous:true, evalScripts:true});; return false;">Gửi wap push</a>
                                        
                                        <?php
//                                        echo link_to_remote('Gửi wap push',
//                                            array(
//                                                'update' => 'sms_count_' . $download[$i]['id'],
//                                                'url' => '/customer_care/resendSms/?id=' . $download[$i]['id'],
//                                                'script' => true,
//                                            )
////                                            array(
////                                                'onclick' => 'return confirm("Bạn có chắc muốn gửi wap push đến '.$download[$i]['to_phone'].'?");',
////                                            )
//                                        );
                                        ?>
                                    </td>
                                    <td >
                                        <div id=<?php echo "sms_count_" . $download[$i]['id']; ?> class=<?php echo "sms_count_" . $download[$i]['id']; ?> >
                                <?php echo $download[$i]['sms_count']; ?>
                                        </div>

                                    </td>
                                </tr>
    <?php endfor; ?>
                        </tbody>
                    </table>
                    <b>Có <?php echo $totalDownload; ?> kết quả</b>
                            <?php if ($totalPage > 1): ?>
                        <div class= "clearfix" align="right">
                            <div class="paging_comment" >
                        <?php
                        $url = "/customer_care/download?phone_number=" . $phoneNumber . "&created_datetime_from=" . $createdDatetimeFrom . "&created_datetime_to=" . $createdDatetimeTo . "&download_source=" . $downloadSource . "&page=%s";
                        echo showPage_1($totalPage, $page, $url);
                        ?>
                            </div>
                        </div>
        <?php endif; ?>
    <?php endif; ?>
            </div>
        </div>
    </div>
    <?php

    function paging($totalPage, $currentPage, $minPage, $previousPage, $nextPage, $maxPage) {
        $minPage = ($currentPage - 3 > 1 ? ($currentPage - 3) : 1);
        $maxPage = ($currentPage + 3 < $totalPage ? ($currentPage + 3) : $totalPage);
        $previousPage = $currentPage - 1;
        $nextPage = $currentPage + 1;
    }

    function showPage_1($totalPage, $page, $url) {
        if ($totalPage > 1) {
            $minPage = 0;
            $maxPage = 0;
            $previousPage = 0;
            $nextPage = 0;
            paging($totalPage, $page, &$minPage, &$previousPage, &$nextPage, &$maxPage);

            $urlToFirst = sprintf($url, 1);
            $urlToPrevious = sprintf($url, $previousPage);
            $urlToNext = sprintf($url, $nextPage);
            $urlToLast = sprintf($url, $totalPage);

            if ($minPage == 1) {
                $linkToFirst = '';
            } else
            if ($minPage == 2) {
                $linkToFirst = '<a href="' . $urlToFirst . '">1</a>'; //link_to(1, $urlToFirst);
            } else {
                $linkToFirst = '<a href="' . $urlToFirst . '">1</a>...'; //link_to(1, $urlToFirst).'...';
            }

            //$linkToPrevious = ($previousPage < 1 ? '' : link_to(PAGE_PREVIOUS, $urlToPrevious));

            if ($maxPage == $totalPage) {
                $linkToLast = '';
            } else
            if ($maxPage == $totalPage - 1) {
                $linkToLast = '<a href="' . $urlToLast . '">' . $totalPage . '</a>'; //link_to($totalPage, $urlToLast);
            } else {
                $linkToLast = '<li>...</li><li><a href="' . $urlToLast . '">' . $totalPage . '</a>'; //link_to($totalPage, $urlToLast).'</li>';
            }

            //$linkToNext = ($nextPage > $totalPage ? '' : link_to(PAGE_NEXT, $urlToNext));
            $linkToPage = '';
            for ($i = $minPage; $i <= $maxPage; $i++) {
                $urlToPage = sprintf($url, $i);
                $linkToPage.=($i == $page ? '<li><a class="page_current">' . $i . '</a></li>' : '<li><a href="' . $urlToPage . '">' . $i . '</a></li>' . '');
            }
            echo '<div>Trang: ' .
            '
	                     <ul>
	                         <li>' . $linkToFirst . '</li>' .
            $linkToPage .
            '<li>' . $linkToLast . '</li>
	                     </ul>
	                   </div>
	               ';
        }
    }
    ?>
